<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public $userType = 'Front';
	private $_id;
	
	public function authenticate()
	{	
		if($this->userType == 'Admin')// This is admin login
        {	
        	Yii::app()->user->setState('isGuest',null);
            // check if login details exists in database
            $record=Users::model()->findByAttributes(array('username'=>$this->username));  // here I use Email as user name which comes from database
            if($record===null)
            { 
                $this->errorCode=self::ERROR_USERNAME_INVALID;
            }
            else if($record->password != md5($this->password)) // let we have md5 password in database
            { 
                $this->errorCode=self::ERROR_PASSWORD_INVALID;
            }
            else
            {  
                Yii::app()->user->setState('isAdmin',1);
                $this->_id = $record->id;
                Yii::app()->user->setState('userId',$record->id);
                Yii::app()->user->setState('role',$record->role);
                Yii::app()->user->setState('email', $record->username);
                $this->errorCode=self::ERROR_NONE;
            }
            return !$this->errorCode;
        }
        if($this->userType == 'Front')// This is admin login
        {
        	Yii::app()->user->setState('isAdmin',null);
        	// check if login details exists in database
        	$record=Users::model()->find(array('condition' => 'username="'.$this->username.'" AND role_id != "1" AND status = "1"'));  // here I use Email as user name which comes from database
        	if($record === null)
        	{
        		$this->errorCode=self::ERROR_USERNAME_INVALID;
        	}
        	else if($record->password != md5($this->password)) // let we have md5 password in database
        	{	
        		if( isset(Yii::app()->session['userActivation']) && (Yii::app()->session['userActivation'] == "logInDirect") ){
        			Yii::app()->user->setState('isGuest',1);
        			$this->_id = $record->id;
        			Yii::app()->user->setState('userId',$record->id);
        			Yii::app()->user->setState('email', $record->username);
        			Yii::app()->user->setState('role', $record->role_id);
        			$this->errorCode=self::ERROR_NONE;
        		} else {
        			$this->errorCode=self::ERROR_PASSWORD_INVALID;
        		}	
        	}
        	else
        	{
        		Yii::app()->user->setState('isGuest',1);
        		$this->_id = $record->id;
        		Yii::app()->user->setState('userId',$record->id);
        		Yii::app()->user->setState('email', $record->username);
        		Yii::app()->user->setState('role', $record->role_id);
        		$this->errorCode=self::ERROR_NONE;
        	}
        	unset(Yii::app()->session['userActivation']);
        	return !$this->errorCode;
        }
	}
	public function getId()
	{
		return $this->_id;
	}
}
